import React, { useCallback, useEffect, useImperativeHandle, useMemo, useRef, useState } from "react";
import { columnsData } from "./ColumnsData";
import { useModel } from "umi";
import FormsTitle from "../components/FormsTitle";
import { getQcbbhdData, getWstzfl } from "../service";
import AdjustmentPopup from "./components/adjustmentPopup";
import notify from "devextreme/ui/notify";
import { Guid } from 'guid-factory/lib/Guid';
import '@/pages/components/StyleFile/btnStyle.less';
import CpasTablePupop from "@/pages/components/CpasTablePupop";
import { CpasIcon, DevGrid, DevToolBar } from "~/public/cpas-ui";
import PageLoading from "@/pages/components/PageLoading";


const OpeningReportForms = React.forwardRef((props: any, ref) => {
    const { slectYearData } = props;
    const { Data_ID, checkBoxVaue } = slectYearData;
    const { initialState } = useModel('@@initialState');
    const { dbname } = initialState?.curPart as API.CurPart || {};
    const DataRef = useRef<any>();
    const [qcbbhdData, setQcbbhdData] = useState<any[]>([]);
    // const [tzflData_ID, setTzflData_ID] = useState<any>(null);
    const tzflData_ID = useRef<any>(null);

    const RefAdjustmentPopup = useRef<any>(0);
    const [spinLoad, setSpinLoad] = useState<boolean>(false);
    const refsCpasTablePupop = useRef<any>();

    function getGridInstance() {
        return DataRef.current.gridRef.current.instance;
    }

    const EntryEditClick = useCallback(async (rowData: any) => {
        if (!tzflData_ID.current) return;
        const data_IDNumber = tzflData_ID.current * 1 - 1;
        const res: any = await getWstzfl({
            dbname,
            Data_ID: data_IDNumber,
            itemName: rowData.item
        });
        if (res.success) {
            const newData = res.data.map((item: any) => {
                item.guidKey = Guid.newGuid();
                return item;
            })
            RefAdjustmentPopup.current.show(newData);
        } else {
            notify(`获取数据失败：${res.data}`, "error", 2000);
        }
    }, []);

    const columns = [
        ...columnsData,
        {
            caption: "查看上期调整",
            width: 120,
            alignment: "center",
            allowResizing: false,
            fixed: true,
            fixedPosition: 'right',
            cellRender: (cell: any) => {
                return cell.data.qccy !== 0 && (<div className='hoverBac' style={{ margin: "0px auto" }}><span><CpasIcon type='icon-rename-copy' fontSize={16} onClick={() => EntryEditClick(cell.data)} /></span></div>)
            }
        },
    ];

    const questQcbbhdData = async (tabs_year_ID: string, tabs_checkBox: boolean) => {
        setSpinLoad(true);
        tzflData_ID.current = 0;
        const res: any = await getQcbbhdData({
            dbname,
            data_ID: tabs_year_ID,
            wcybxs: tabs_checkBox
        });
        if (res.success) {
            tzflData_ID.current = tabs_year_ID;
            getGridInstance().pageIndex(0);
            setQcbbhdData(res.data);
        } else {
            notify(`获取数据失败：${res.data}`, "error", 2000);
        }
        setSpinLoad(false);
    }



    // 自定义方法
    useImperativeHandle(ref, () => ({
        reloadDevTable: (tabs_year_ID: any, tabs_checkBox: boolean) => {
            questQcbbhdData(tabs_year_ID, tabs_checkBox);
        },
    }));



    useEffect(() => {
        if (Data_ID) {
            questQcbbhdData(Data_ID, checkBoxVaue);
        }
    }, [Data_ID, checkBoxVaue]);




    return (<div style={{ height: "calc(100% - 45px)" }}>
        {/* {spinLoad && <SpinLoading loadString="" />} */}
        <PageLoading
            isOpen={spinLoad}
            isMaskClose={false}
            content={""}
            width={200}
        />

        {/* <FormsTitle tabsData={{ tabsTitle: '期初报表核对' }}></FormsTitle> */}
        <DevGrid.RangeTap
            width="100%"
            height="100%"
            dataSource={qcbbhdData}
            // rowAlternationEnabled={true}
            ref={DataRef}
            keyExpr="xh"
            className="devHeadersCenter PaperDevSortableGrid "
            defaultColumns={columns}
            sheetName="期初报表核对"
            excelName="期初报表核对"
            showBorders={true}
            showRowLines={true}
            showColumnLines={true}
            paging={{
                enabled: false
            }}
        />
        <AdjustmentPopup ref={RefAdjustmentPopup} currentSjnd={Data_ID} reloadDevTable={() => questQcbbhdData(Data_ID, checkBoxVaue)} />

        <CpasTablePupop
            ref={refsCpasTablePupop}
            reloadDevTable={() => {
                questQcbbhdData(Data_ID, checkBoxVaue)
            }}
        />
    </div>
    )
})
export default OpeningReportForms;
